//
//  LHYSignAlertView.m
//  DrawLine
//
//  Created by lhy on 2021/1/21.
//

#import "LHYSignAlertView.h"
#import "LHYDefine.h"

@interface LHYSignAlertView ()
{
    CGPoint points[5];
}
@property(nonatomic,assign) NSInteger control;
@property(nonatomic,strong) UIBezierPath *beizerPath;
@property (weak, nonatomic) IBOutlet UIView *signView;
@property (weak, nonatomic) IBOutlet UIButton *btnCancle;
@property (weak, nonatomic) IBOutlet UIButton *btnOK;
@property (weak, nonatomic) IBOutlet UIButton *btnClear;

@end

@implementation LHYSignAlertView

+ (instancetype)shareSignView {
    LHYSignAlertView *view = [[[NSBundle mainBundle] loadNibNamed:@"LHYSignAlertView" owner:nil options:nil] lastObject];
    view.frame = CGRectMake((kScreenSizeWidth - 1000) / 2,
                            kScreenSizeHeight - 700 - kNavBarAndStatusBarHeight,
                            500,
                            400);
    return view;
}

- (void)awakeFromNib {
    [super awakeFromNib];
    
    [self setMultipleTouchEnabled:NO];
    self.beizerPath = [UIBezierPath bezierPath];
    [UIColor.blackColor setStroke];// 线条颜色
    [self.beizerPath setLineWidth:4];//线宽
    self.beizerPath.lineCapStyle = kCGLineCapRound; //线条终点
    self.beizerPath.lineJoinStyle = kCGLineJoinRound; //线条拐角
}

#pragma mark - 绘图操作

- (void)drawRect:(CGRect)rect {
    //签名的路径绘制
    [self.beizerPath stroke];
}

- (void)show {
    UIWindow *window = [[UIApplication sharedApplication] windows].firstObject;
    [UIView animateWithDuration:0.2 animations:^{
        [window addSubview:self];
    } completion:^(BOOL finished) {
        
    }];
}

- (void)hide {
    [self removeFromSuperview];
}

- (IBAction)actionBtnItem:(UIButton *)sender {
    if ([sender isEqual:self.btnCancle]) {
        [self hide];
    } else if ([sender isEqual:self.btnOK]) {
        [self saveSignImage];
    } else if ([sender isEqual:self.btnClear]) {
        [self.beizerPath removeAllPoints];
        [self setNeedsDisplay];
    }
}

- (void)saveSignImage {
    
    if (self.getSignImageBlock) {
        [self hide];
        self.getSignImageBlock([self getSignatureImage]);
    }
}

#pragma mark - 获取图片
- (UIImage *)getSignatureImage {
    
    // 绘图前，移除多余的内容
    self.navView.hidden = YES;
    self.backgroundColor = UIColor.clearColor;
    self.layer.borderWidth = 0;
    
    // 设置为NO，UIView是透明这里的图片就是透明的
    UIGraphicsBeginImageContextWithOptions(self.bounds.size, NO, [UIScreen mainScreen].scale);
    [self.layer renderInContext:UIGraphicsGetCurrentContext()];
    
    UIImage *signatureImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    // 保存图片到本地
    //    NSString* docDir = [NSString stringWithFormat:@"%@/Documents/Image", NSHomeDirectory()];
    //    [[NSFileManager defaultManager] createDirectoryAtPath:docDir withIntermediateDirectories:YES attributes:nil error:nil];
    //    NSString *path = [NSString stringWithFormat:@"%@/Documents/Image/IMAGE.PNG", NSHomeDirectory()];
    //    //用png是透明的
    //    [UIImagePNGRepresentation(signatureImage) writeToFile: path atomically:YES];
    return signatureImage;
}

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
    
    self.control = 0;
    UITouch *touch = touches.anyObject;
    points[0] = [touch locationInView:self];
    [self.beizerPath moveToPoint:points[0]];
}

- (void)touchesMoved:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
    
    UITouch *touch = touches.anyObject;
    CGPoint touchPoint = [touch locationInView:self];
    
    _control++;
    points[_control] = touchPoint;
    
    if (_control == 4) {
        points[3] = CGPointMake((points[2].x + points[4].x) / 2.0,
                                (points[2].y + points[4].y) / 2.0);
        
        //设置画笔起始点
        [self.beizerPath moveToPoint:points[0]];
        
        //endPoint终点 controlPoint1、controlPoint2控制点
        [self.beizerPath addCurveToPoint:points[3]
                           controlPoint1:points[1]
                           controlPoint2:points[2]];
        
        //setNeedsDisplay会自动调用drawRect方法，这样可以拿到UIGraphicsGetCurrentContext，就可以画画了
        [self setNeedsDisplay];
        
        points[0] = points[3];
        points[1] = points[4];
        _control = 1;
    }
}



@end




//    简单绘线
//    CGContextRef context = UIGraphicsGetCurrentContext();
//    CGContextSetLineWidth(context, 3);
//    CGContextBeginPath(context);
//    CGContextMoveToPoint(context, 0, 0);
//
//    CGContextAddLineToPoint(context, 50, 100);
//    CGContextAddLineToPoint(context, 100, 110);
//
//    CGContextStrokePath(context);
